//
//  LXKRadioPlayController.m
//  EPA
//
//  Created by lanou3g on 16/4/21.
//  Copyright © 2016年 LXK. All rights reserved.
//

#import "LXKRadioPlayController.h"
#import "LXKRadioListModel.h"
#import "LXKAVManager.h"

@interface LXKRadioPlayController ()
//标题
@property (strong, nonatomic) IBOutlet UILabel *titleLabel;
//图片
@property (strong, nonatomic) IBOutlet UIImageView *coverImageView;
//当前时间
@property (strong, nonatomic) IBOutlet UILabel *currentLabel;
//播放进度
@property (strong, nonatomic) IBOutlet UISlider *rateSlider;
//总时间
@property (strong, nonatomic) IBOutlet UILabel *totalLabel;
//计时器
@property (nonatomic, strong) NSTimer *playTimer;

@property (weak, nonatomic) IBOutlet UIButton *playButton;

@end

@implementation LXKRadioPlayController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.navigationController.navigationBar.translucent = NO;
    LXKRadioListModel *model = self.radioArray[self.currentIndex];
    self.titleLabel.text = model.title;
    [self.coverImageView sd_setImageWithURL:[NSURL URLWithString:model.coverimg]];
    
    [LXKAVManager shareMabager].currentIndex = self.currentIndex;
    
    NSMutableArray *itemArray = [NSMutableArray new];
    for (LXKRadioListModel *model in self.radioArray) {
        NSURL *musicUrl = [NSURL URLWithString:model.playInfoModel.musicUrl];
        NSLog(@"===%@",musicUrl);
        AVPlayerItem *item = [AVPlayerItem playerItemWithURL:musicUrl];
        [itemArray addObject:item];
    }
    //AVplayerItem数组
    [LXKAVManager shareMabager].itemArray = itemArray;
    NSLog(@"音乐数组%@",[LXKAVManager shareMabager].itemArray);
    [[LXKAVManager shareMabager] play];
    
    //创建Timer
    self.playTimer = [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(timerAction) userInfo:nil repeats:YES];
    
    
    
   
    
    // Do any additional setup after loading the view from its nib.
}

- (void)timerAction {
    float current = [LXKAVManager shareMabager].player.currentTime.value / [LXKAVManager shareMabager].player.currentTime.timescale;
    NSString *currentStr = [NSString stringWithFormat:@"%02ld:%02ld",(NSInteger)current/60,(NSInteger)current%60];
    self.currentLabel.text = currentStr;

    float total = 0;
    if ([LXKAVManager shareMabager].player.currentItem.duration.timescale > 0) {
        total = [LXKAVManager shareMabager].player.currentItem.duration.value / [LXKAVManager shareMabager].player.currentItem.duration.timescale;
    }
    NSString *totalStr = [NSString stringWithFormat:@"%02ld:%02ld",(NSInteger)total/60,(NSInteger)total%60];
    self.totalLabel.text = totalStr;
    if (total > 0) {
        float rate = current / total;
        self.rateSlider.value = rate;
    }
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
//上一首
- (IBAction)didClickPreviousButton:(id)sender {
    [[LXKAVManager shareMabager] previous];
    //图片标题
    self.currentIndex--;
    if (self.currentIndex < 0) {
        self.currentIndex = self.radioArray.count - 1;
    }
    LXKRadioListModel *model = self.radioArray[self.currentIndex];
    [self.coverImageView sd_setImageWithURL:[NSURL URLWithString:model.coverimg]];
    self.titleLabel.text = model.title;
    [self.playButton setImage:[UIImage imageNamed:@"播放"] forState:UIControlStateNormal];
}
//暂停播放
- (IBAction)didClickPlayButton:(id)sender {
    if ([LXKAVManager shareMabager].player.rate == 0) {
        [[LXKAVManager shareMabager]play];
        [(UIButton *)sender setImage:[UIImage imageNamed:@"播放.png"] forState:UIControlStateNormal];
    }else {
        [[LXKAVManager shareMabager] pause];
         [(UIButton *)sender setImage:[UIImage imageNamed:@"暂停"] forState:UIControlStateNormal];
    }
}
//下一首
- (IBAction)didClickPlayfollowingpieceButton:(id)sender {
    [[LXKAVManager shareMabager] next];
    //图片标题
    self.currentIndex ++;
    if (self.currentIndex >= self.radioArray.count) {
        self.currentIndex = 0;
    }
    LXKRadioListModel *model = self.radioArray[self.currentIndex];
    [self.coverImageView sd_setImageWithURL:[NSURL URLWithString:model.coverimg]];
    self.titleLabel.text = model.title;
    [self.playButton setImage:[UIImage imageNamed:@"播放"] forState:UIControlStateNormal];
}

//改变播放进度
- (IBAction)didChangeRateAction:(UISlider *)sender {
    CMTime changeTime = CMTimeMake(sender.value * [LXKAVManager shareMabager].player.currentItem.duration.value, [LXKAVManager shareMabager].player.currentItem.duration.timescale);
    //seekToTime
    [[LXKAVManager shareMabager] seekToTime:changeTime];
}


/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
